/*
 *  tf_dialog.cpp
 *  Perceel
 *
 *  Created by Nicola Gigante on 19/03/08.
 *  Copyright 2008 Nicola Gigante. All rights reserved.
 *
 */

#include "tf_dialog.h"

TFDialog::TFDialog(QWidget *parent) : QDialog(parent, Qt::Sheet)	
{
	setupUi(this);
	
	tfRenderer->setTF(m_tf);
	
	is_selecting = false;
	
	connect(addRootButton, SIGNAL(clicked()), this, SLOT(addRoot()));
	connect(addPoleButton, SIGNAL(clicked()), this, SLOT(addPole()));
	connect(removeButton, SIGNAL(clicked()), this, SLOT(remove()));
	connect(tfRenderer, SIGNAL(selectionChanged()), this, SLOT(selection()));
	connect(realSpin, SIGNAL(valueChanged(double)), this, SLOT(valueChanged()));
	connect(imagSpin, SIGNAL(valueChanged(double)), this, SLOT(valueChanged()));
	connect(orderSpin, SIGNAL(valueChanged(int)), this, SLOT(valueChanged()));
}

void TFDialog::addRoot()
{
	Complex c(realSpin->value(), imagSpin->value());
	
	for(int i = 0; i < orderSpin->value(); i++)
		m_tf.addRoot(c);
	
	tfRenderer->setTF(m_tf);
}

void TFDialog::addPole()
{
	Complex c(realSpin->value(), imagSpin->value());
	
	for(int i = 0; i < orderSpin->value(); i++)
		m_tf.addPole(c);
	
	tfRenderer->setTF(m_tf);
}

void TFDialog::selection()
{
	Complex p;
	int order = 1;
	bool selected = false;
	
	if(tfRenderer->selectedRoot() >= 0)
	{
		p = m_tf.roots()[tfRenderer->selectedRoot()];
		order = m_tf.roots().count(p);
		selected = true;
	}
	if(tfRenderer->selectedPole() >= 0)
	{
		p = m_tf.poles()[tfRenderer->selectedPole()];
		order = m_tf.poles().count(p);
		selected = true;
	}
	
	addRootButton->setDisabled(selected);
	addPoleButton->setDisabled(selected);
	removeButton->setDisabled(!selected);
	
	is_selecting = true;
	realSpin->setValue(real(p));
	imagSpin->setValue(imag(p));
	orderSpin->setValue(order);
	is_selecting = false;
}

void TFDialog::valueChanged()
{
	if(is_selecting)
		return;
	Complex c(realSpin->value(), imagSpin->value());
	
	if(tfRenderer->selectedRoot() >= 0)
		m_tf.setRoot(tfRenderer->selectedRoot(), c, orderSpin->value());
	if(tfRenderer->selectedPole() >= 0)
		m_tf.setPole(tfRenderer->selectedPole(), c, orderSpin->value());
	
	tfRenderer->setTF(m_tf);
}

void TFDialog::remove()
{
	if(tfRenderer->selectedRoot() >= 0)
		m_tf.removeRoot(tfRenderer->selectedRoot());
	if(tfRenderer->selectedPole() >= 0)
		m_tf.removePole(tfRenderer->selectedPole());
	
	tfRenderer->setTF(m_tf);
}



